package com.wudi.estate.controller.controller;


import com.github.pagehelper.PageInfo;
import com.wudi.estate.bean.admin.Admin;
import com.wudi.estate.bean.admin.Role;
import com.wudi.estate.service.admin.AdminService;
import com.wudi.estate.service.admin.RoleService;
import com.wudi.estate.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author luck
 * @since 2022-10-22
 */
@SessionAttributes(value = "admin")
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    @RequestMapping("/loginUi")
    public String loginUi(){
        System.out.println("进入loginUi");
        return "login";
    }
    @RequestMapping("/login")
    public String login(){
        return "index";
    }

    @RequestMapping("/index")
    public String index(ModelMap map){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        System.out.println(username);
        System.out.println("进入index");
        Admin admin = null;
        try {
            admin = adminService.selectOneByName(username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("admin",admin);
        System.out.println(admin);
        return "index";
    }
    @RequestMapping("/welcome")
    public String welcome(){
        return "welcome";
    }
    @RequestMapping("logout")
    public String loginout(HttpSession httpSession){

        httpSession.removeAttribute("admin");
        return "redirect:/admin/loginUi";
    }
    @RequestMapping("unAuth")
    public String unAuth(){
        return "unauth";
    }



    // 管理员列表画面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map, Integer pageNum) {

        if (pageNum == null) {
            pageNum = 1;
        }

        try {
            // 假设第一个启动页面， 当前页数1，每页显示的数量3
            // 通过业务层获得分页信息
            PageInfo<Map<String, Object>> pageInfo
                    = adminService.getPageInfo(pageNum, 3);  // 参数1=当前页，参数2=每页显示数量

            map.addAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "admin_list";
    }

    // 添加画面
    @RequestMapping("/addUi")
    public String addUi() {
//        try {
//            List<Map<String, Object>> admins = roleService.selectAll();
//            map.addAttribute("admins", admins);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return "admin_add";
    }

    // 添加
    @RequestMapping("/add")
    public String add(Admin admin, MultipartFile headerFile) {
        System.out.println("admin = " + admin);
        String fileName = "";
        try {
            fileName = FileUtils.fileUpload(headerFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 设置数据
        admin.setCreateTime(new Date());
/*        // 加密
        // 设置随机盐
        String salt = SecurityUils.getSalt();
        admin.setSalt(salt);
        // 设置盐加密后的密码
        String password = SecurityUils.getMD5Password(admin.getPassword(), salt);
        // 重新设置加密后的密码
        admin.setPassword(password);*/

        try {
            adminService.insert1(admin);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "redirect:/admin/listUi";
    }
    @RequestMapping("/editUi")
    public String edit(ModelMap map,long id){
        return "admin_edit";
    }

}

